var generatedId = 10000;
var generatedNodeId = 10000;
var listResponse = {
    "_embedded": {
      "trees": [{
        "name": "tree-one",
        "id": 401,
        "localedName": "用户组",
        "description": "树状结构的用户组",
        "_links": {
          "self": {
            "href": "http://localhost/api/v1/trees/401"
          },
          "treeBlob": {
            "href": "http://localhost/api/v1/trees/401{?projection}",
            "templated": true
          }
        }
      }, {
        "name": "tree-two",
        "id": 402,
        "localedName": "部门",
        "description": "树状结构的部门",
        "_links": {
          "self": {
            "href": "http://localhost/api/v1/trees/402"
          },
          "treeBlob": {
            "href": "http://localhost/api/v1/trees/402{?projection}",
            "templated": true
          }
        }
      }]
    },
    "_links": {
      "self": {
        "href": "http://localhost/api/v1/trees"
      },
      "profile": {
        "href": "http://localhost/api/v1/profile/trees"
      },
      "search": {
        "href": "http://localhost/api/v1/trees/search"
      }
    },
    "page": {
      "size": 10,
      "totalElements": 2,
      "totalPages": 1,
      "number": 0
    }
  };

  var oneResponse = {
    "name": "tree-one",
    "id": 401,
    "localedName": "用户组",
    "description": "树状结构的用户组",
    "treeContent": "{\"jsonLength\":0,\"treeName\":\"tree-one\",\"root\":{\"id\":301,\"n\":\"ROOT\",\"v\":null,\"c\":[{\"id\":305,\"n\":\"tl1\",\"v\":null,\"c\":[],\"p\":301}, {\"id\":302,\"n\":\"ol\",\"v\":null,\"c\":[{\"id\":304,\"n\":\"tl1\",\"v\":null,\"c\":[],\"p\":302}, {\"id\":303,\"n\":\"tl\",\"v\":null,\"c\":[],\"p\":302}],\"p\":301}],\"p\":null}}",
    "_links": {
      "self": {
        "href": "http://localhost/api/v1/trees/401"
      },
      "treeBlob": {
        "href": "http://localhost/api/v1/trees/401{?projection}",
        "templated": true
      }
    }
  };

function _genereateTreeContent(item) {
  ++generatedNodeId;
  var tc = JSON.parse(oneResponse.treeContent);
  return JSON.stringify(tc);
}

function TreeFixture() {
  this.listResponse = listResponse;
  this.oneResponse = oneResponse;
}

function _pushToList(item) {
  listResponse._embedded.trees.push(item);
  listResponse.page.totalElements++;
}

function createNew(body) {
  ++generatedId;
  var n = {
    name: body.name,
    id: generatedId,
    localedName: body.localedName,
    description: body.description,
    _links: {
      self: {
        href: "http://localhost/api/v1/trees/" + generatedId
      },
      treeBlob: {
        "href": "http://localhost/api/v1/trees/" + generatedId + "{?projection}",
        "templated": true
      }
    }
  };
  _pushToList(n);
  return n;
}

function updateOne(id, body) {
  var item = find(id);
  item.name = body.name;
  item.localedName = body.localedName;
  item.description = body.description;
  return item;
}

function find(id) {
  var i = 0,
      list = listResponse._embedded.trees;
  for(;i < list.length; i++) {
    if (list[i].id == id) {
      var node = list[i];
      if (!node.treeContent) {
        node.treeContent = _genereateTreeContent();
      }
      return node;
    }
  }
}

TreeFixture.prototype.createNew = createNew;
TreeFixture.prototype.find = find;
TreeFixture.prototype.updateOne = updateOne;


module.exports = new TreeFixture();
